package cn.jsl.pet.service.impl;

import cn.jsl.basic.exception.MyException;
import cn.jsl.basic.query.BaseQuery;
import cn.jsl.basic.service.IBaiduAiAuditService;
import cn.jsl.basic.service.impl.BaseServiceImpl;
import cn.jsl.basic.util.LoginContext;
import cn.jsl.basic.util.PageList;
import cn.jsl.emp.domain.Employee;
import cn.jsl.emp.mapper.EmployeeMapper;
import cn.jsl.logininfo.domain.Logininfo;
import cn.jsl.pet.domain.Pet;
import cn.jsl.pet.domain.PetDetail;
import cn.jsl.pet.domain.PetOnlineAuditLog;
import cn.jsl.pet.mapper.PetDetailMapper;
import cn.jsl.pet.mapper.PetMapper;
import cn.jsl.pet.mapper.PetOnlineAuditLogMapper;
import cn.jsl.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jijie
 * @since 2022-06-15
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Override
    public void save(Pet pet) {
        super.save(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    public void delete(Long id) {
        super.delete(id);
        petDetailMapper.delPetDetailByPetId(id);
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    /**
     * 宠物上架
     * @param ids
     * @param request
     */
    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        //获取登录信息对象
        Logininfo logininfo = LoginContext.getLoginUser(request);
        //获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            //上架之前对文本和图片做审核
            Boolean textBoo = baiduAiAuditService.textAudit(pet.getName());
            Boolean imageBoo = baiduAiAuditService.imageAudit(pet.getResources());
            if(textBoo && imageBoo){
                //审核通过：修改状态 + 添加上架的审核成功日志
                pet.setOnsaletime(new Date());
                pet.setState(1);
                petMapper.update(pet);
                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(1);//1审核通过  0待审核  -1驳回
                log.setAuditTime(new Date());
                log.setNote("审核成功");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
            }else{
                //审核失败：添加上架的审核失败日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(-1);//1审核通过  0待审核  -1驳回
                log.setAuditTime(new Date());
                log.setNote("审核失败,内容不合法!");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
                throw new MyException("审核失败,内容不合法!");
            }
        }
    }

    /**
     * 宠物下架
     * @param ids
     * @param
     */
    @Override
    public void offsale(Long[] ids) {
        Date date = new Date();
        petMapper.offsale(date,ids);
    }

}
